package com.bestcem.xm.txtanls.grpc.convert;

import cn.hutool.core.lang.Validator;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.txtanls.grpc.service.param.chart.*;
import com.bestcem.xm.txtanls.grpc.v1.common.PeriodTime;
import com.bestcem.xm.txtanls.grpc.v1.common.TimeRange;
import com.bestcem.xm.txtanls.grpc.v1.enums.EvaluationTypeEnum;
import com.bestcem.xm.txtanls.grpc.v1.enums.IntervalEnum;
import com.bestcem.xm.txtanls.grpc.v1.enums.SortKeyEnum;
import com.bestcem.xm.txtanls.grpc.v1.services.*;
import com.bestcem.xm.txtanls.service.dto.chart.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 结果转换为GRpc结果
 *
 * @author jy.zhao
 * @version 1.0
 * @date 2021/6/7 14:24
 **/
public class ChartConvert {


    /**
     * 根据观点评价请求信息转换为观点统计参数param
     *
     * @param request 观点评价请求信息
     * @return ViewEvaluateParam 观点统计参数param
     * @author jy.zhao
     * @date 2021/5/13 14:04
     **/
    public static ViewpointEvaluateParam requestToViewEvaluateParam(GetViewpointEvaluateRequest request) {
        ViewpointEvaluateParam viewpointEvaluateParam = new ViewpointEvaluateParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(request.getBaseChart(), viewpointEvaluateParam);
        // 默认排序规则为 比上期：由大到小
        if (request.getSortKey().getNumber() == 0) {
            viewpointEvaluateParam.setSortKey(SortKeyEnum.SortKey.COMPARE_PREVIOUS.name());
        } else {
            viewpointEvaluateParam.setSortKey(request.getSortKey().name());
        }
        // 评价类型默认为好评
        if (request.getEvaluationType().getNumber() == 0) {
            viewpointEvaluateParam.setEvaluationType(EvaluationTypeEnum.EvaluationType.GOOD_VALUE);
        } else {
            viewpointEvaluateParam.setEvaluationType(request.getEvaluationType().getNumber());
        }
        viewpointEvaluateParam.setViewpointType(request.getViewpointType().getNumber());
        viewpointEvaluateParam.setViewpointGid(request.getViewpointGid());

        return viewpointEvaluateParam;
    }

    /**
     * 根据词云图请求信息转换为词云图参数param
     *
     * @param request 词云图请求
     * @return WorkCloudParam
     * @author jy.zhao
     * @date 2021/5/14 14:22
     **/
    public static WorkCloudParam requestToWorkCloudParam(GetWordCloudRequest request) {
        WorkCloudParam workCloudParam = new WorkCloudParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(request.getBaseChart(), workCloudParam);
        workCloudParam.setTopicId(request.getTopicId());

        return workCloudParam;
    }

    /**
     * param转换为话题引用率
     *
     * @param request 请求
     * @return TopicReferenceParam 返回
     * @author jy.zhao
     * @date 2021/7/27 21:55
     **/
    public static TopicReferenceParam requestToTopicReferenceRateParam(GetTopicReferenceRateRequest request) {
        TopicReferenceParam topicReferenceRateParam = new TopicReferenceParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(request.getBaseChart(), topicReferenceRateParam);
        // 默认排序规则为 比上期：由大到小
        if (request.getSortKey().getNumber() == 0) {
            topicReferenceRateParam.setSortKey(SortKeyEnum.SortKey.COMPARE_PREVIOUS.name());
        } else {
            topicReferenceRateParam.setSortKey(request.getSortKey().name());
        }
        topicReferenceRateParam.setTopicGid(request.getTopicGid());
        topicReferenceRateParam.setTopicType(request.getTopicType().getNumber());

        return topicReferenceRateParam;
    }

    /**
     * param转换为话题关联
     *
     * @param request 请求
     * @return TopicRelationParam 返回
     * @author jy.zhao
     * @date 2021/7/27 21:56
     **/
    public static TopicRelationParam requestToTopicRelationParam(GetTopicRelationRequest request) {
        TopicRelationParam topicRelationParam = new TopicRelationParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(request.getBaseChart(), topicRelationParam);
        topicRelationParam.setTopicId(request.getTopicId());

        return topicRelationParam;
    }

    /**
     * request请求中的参数转换为话题提及率变化趋势参数
     *
     * @param request 请求
     * @return TopicReferenceTrendParam 返回
     * @author jy.zhao
     * @date 2021/7/27 21:53
     **/
    public static TopicReferenceTrendParam requestToTopicReferenceTrendParam(GetTopicReferenceTrendRequest request) {
        TopicReferenceTrendParam topicsReferenceTrendParam = new TopicReferenceTrendParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(request.getBaseChart(), topicsReferenceTrendParam);
        topicsReferenceTrendParam.setTopicId(request.getTopicId());
        topicsReferenceTrendParam.setTopicGid(request.getTopicGid());

        return topicsReferenceTrendParam;
    }

    /**
     * request请求中的参数转换为观点变化趋势参数
     *
     * @param request 请求
     * @return ViewpointEvaluateTrendParam 返回
     * @author jy.zhao
     * @date 2021/7/27 21:55
     **/
    public static ViewpointEvaluateTrendParam requestToViewpointEvaluateTrendParam(GetViewpointEvaluateTrendRequest request) {
        ViewpointEvaluateTrendParam viewpointEvaluateTrendParam = new ViewpointEvaluateTrendParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(request.getBaseChart(), viewpointEvaluateTrendParam);
        viewpointEvaluateTrendParam.setViewGid(request.getViewGid());
        viewpointEvaluateTrendParam.setViewId(request.getViewId());

        return viewpointEvaluateTrendParam;
    }

    /**
     * request请求中的参数转换为定义的文本分析基础参数
     *
     * @param baseChartRequest 基础图表请求
     * @param baseChartParam   封装为BaseChartParam
     * @author jy.zhao
     * @date 2021/5/17 16:13
     **/
    private static void requestToBaseChartParam(BaseChartRequest baseChartRequest, BaseChartParam baseChartParam) {
        baseChartParam.setProjectId(baseChartRequest.getProjectId());
        baseChartParam.setFieldId(baseChartRequest.getFieldId());
        // 设置请求的开始结束时间和时间间隔
        baseChartParam.setTimeRangeDTO(timeRangeToTimeRangeDTO(baseChartRequest.getTimeRange()));
    }

    /**
     * request请求中的参数转换为定义的文本分析基础参数
     *
     * @param baseChartRequest 基础图表请求
     * @return BaseChartParam
     * @author jy.zhao
     * @date 2021/5/13 13:44
     **/
    public static BaseChartParam requestToBaseChartParam(BaseChartRequest baseChartRequest) {
        BaseChartParam baseChartParam = new BaseChartParam();
        // request请求中的参数转换为定义的文本分析基础参数
        requestToBaseChartParam(baseChartRequest, baseChartParam);
        return baseChartParam;
    }

    /**
     * 文本分析时间范围转换为TimeRangeDTO
     *
     * @param timeRange grpc中的时间范围
     * @return TimeRangeDTO 时间范围，包括时间间隔和周期开始、结束时间
     * @author jy.zhao
     * @date 2021/5/13 13:42
     **/
    public static TimeRangeDTO timeRangeToTimeRangeDTO(TimeRange timeRange) {
        TimeRangeDTO timeRangeDTO = new TimeRangeDTO();
        if (Validator.isEmpty(timeRange)) {
            return timeRangeDTO;
        }
        PeriodTimeDTO periodTimeDTO = new PeriodTimeDTO();
        if (timeRange.hasPeriodTime()) {
            if (timeRange.getPeriodTime().hasStartTime()) {
                periodTimeDTO.setStartTime(DateUtil.getLocalDateTime(timeRange.getPeriodTime().getStartTime()));
            }
            if (timeRange.getPeriodTime().hasEndTime()) {
                periodTimeDTO.setEndTime(DateUtil.getLocalDateTime(timeRange.getPeriodTime().getEndTime()));
            }
        }
        // 组织时间间隔参数，如果没有传入，默认为天
        if (timeRange.getInterval().getNumber() == IntervalEnum.Interval.UNSPECIFIED_VALUE) {
            timeRangeDTO.setInterval(IntervalEnum.Interval.DAY_VALUE);
        } else {
            timeRangeDTO.setInterval(timeRange.getInterval().getNumber());
        }
        timeRangeDTO.setPeriodTime(periodTimeDTO);
        return timeRangeDTO;
    }

    /**
     * 观点组统计转换为grpc结果
     *
     * @param viewpointGroupStatDTOs 观点组统计结果
     * @return List<ViewpointGroupStat> grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:38
     **/
    public static List<ViewpointGroupStat> viewpointStatToGrpcResult(List<ViewpointGroupStatDTO> viewpointGroupStatDTOs) {
        List<ViewpointGroupStat> viewpointGroupStats = new ArrayList<>();
        // 观点统计结果
        for (ViewpointGroupStatDTO viewpointGroupStatDTO : viewpointGroupStatDTOs) {
            ViewpointGroupStat.Builder builder = ViewpointGroupStat.newBuilder();
            // 观点组统计结果转换
            ViewpointStat viewpointGroupStat = viewpointStatToGrpcResult(viewpointGroupStatDTO.getViewpointGroupStat());
            builder.setViewpointGroupStat(viewpointGroupStat);
            // 子观点统计结果转换
            for (ViewpointStatDTO viewpointStatDTO : viewpointGroupStatDTO.getViewpointStats()) {
                ViewpointStat viewpointStat = viewpointStatToGrpcResult(viewpointStatDTO);
                builder.addViewpointStats(viewpointStat);
            }
            viewpointGroupStats.add(builder.build());
        }
        return viewpointGroupStats;
    }

    /**
     * 观点统计转换为grpc结果
     *
     * @param viewpointStatDTO 观点统计结果
     * @return ViewpointStat grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:38
     **/
    public static ViewpointStat viewpointStatToGrpcResult(ViewpointStatDTO viewpointStatDTO) {
        ViewpointStat.Builder builder = ViewpointStat.newBuilder();
        builder.setId(viewpointStatDTO.getId()).setName(viewpointStatDTO.getName())
                .setNeuNum(viewpointStatDTO.getNeuNum()).setNeuRatio(viewpointStatDTO.getNeuRatio())
                .setNegNum(viewpointStatDTO.getNegNum()).setNegRatio(viewpointStatDTO.getNegRatio())
                .setPosNum(viewpointStatDTO.getPosNum()).setPosRatio(viewpointStatDTO.getPosRatio())
                .setNum(viewpointStatDTO.getNum()).setRatio(viewpointStatDTO.getRatio());
        return builder.build();
    }

    /**
     * 观点评价结果转换为grpc结果
     *
     * @param viewPointEvaluateDTO 观点评价结果
     * @return ViewpointEvaluate grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:39
     **/
    public static ViewpointEvaluate viewpointEvaluateToGrpcResult(ViewPointEvaluateDTO viewPointEvaluateDTO) {
        // 转换本期、上期时间
        ViewpointEvaluate.Builder builder = ViewpointEvaluate.newBuilder().setCurPeriod(periodTimeDTOToGrpcResult(viewPointEvaluateDTO.getCurPeriod()))
                .setLastPeriod(periodTimeDTOToGrpcResult(viewPointEvaluateDTO.getLastPeriod()));
        // 转换返回数据
        List<ViewPointEvaluateRatioDTO> viewRatios = viewPointEvaluateDTO.getViewRatios();
        for (ViewPointEvaluateRatioDTO viewPointEvaluateRatioDTO : viewRatios) {
            builder.addViewpointRatios(viewPointEvaluateRadioDTOToGrpcResult(viewPointEvaluateRatioDTO));
        }
        return builder.build();
    }

    /**
     * 观点计算结果转换为grpc结果
     *
     * @param viewPointEvaluateRatioDTO 观点计算结果
     * @return ViewpointRatio grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:40
     **/
    public static ViewpointRatio viewPointEvaluateRadioDTOToGrpcResult(ViewPointEvaluateRatioDTO viewPointEvaluateRatioDTO) {
        ViewpointRatio.Builder builder = ViewpointRatio.newBuilder();
        builder.setId(viewPointEvaluateRatioDTO.getId()).setName(viewPointEvaluateRatioDTO.getName())
                .setCurNum(viewPointEvaluateRatioDTO.getCurNum())
                .setCurTotalNum(viewPointEvaluateRatioDTO.getCurTotalNum())
                .setCurRatio(viewPointEvaluateRatioDTO.getCurRatio())
                .setLastNum(viewPointEvaluateRatioDTO.getLastNum())
                .setLastTotalNum(viewPointEvaluateRatioDTO.getLastTotalNum())
                .setLastRatio(viewPointEvaluateRatioDTO.getLastRatio())
                .setGrowth(viewPointEvaluateRatioDTO.getGrowth());
        return builder.build();
    }

    /**
     * 情感详情结果转换为grpc结果
     *
     * @param emotionDetailDTO 情感详情结果
     * @return EmotionDetail grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:40
     **/
    public static EmotionDetail emotionDetailDTOToGrpcResult(EmotionDetailDTO emotionDetailDTO) {
        EmotionDetail.Builder builder = EmotionDetail.newBuilder().setLastPeriod(periodTimeDTOToGrpcResult(emotionDetailDTO.getLastPeriod()))
                .setCurPeriod(periodTimeDTOToGrpcResult(emotionDetailDTO.getCurPeriod()))
                .setCurEmotion(emotionDTOToGrpcResult(emotionDetailDTO.getCurEmotion()))
                .setLastEmotion(emotionDTOToGrpcResult(emotionDetailDTO.getLastEmotion()))
                .setGrowth(emotionDTOToGrpcResult(emotionDetailDTO.getGrowth()))
                .setGrowthRatio(emotionDTOToGrpcResult(emotionDetailDTO.getGrowthRatio()));
        return builder.build();
    }

    /**
     * 情感详情结果转换为grpc结果
     *
     * @param emotionDTOs 情感详情结果
     * @return List<Emotion> grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:41
     **/
    public static List<Emotion> emotionDTOToGrpcResult(List<EmotionDTO> emotionDTOs) {
        List<Emotion> emotionList = new ArrayList<>();
        for (EmotionDTO emotionDTO : emotionDTOs) {
            emotionList.add(emotionDTOToGrpcResult(emotionDTO));
        }
        return emotionList;
    }

    /**
     * 情感统计结果转换为grpc结果
     *
     * @param emotionDTO 情感统计结果
     * @return Emotion grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:41
     **/
    public static Emotion emotionDTOToGrpcResult(EmotionDTO emotionDTO) {
        Emotion.Builder builder = Emotion.newBuilder()
                .setMixture(emotionDTO.getMixture())
                .setNegative(emotionDTO.getNegative())
                .setNeutral(emotionDTO.getNeutral())
                .setPositive(emotionDTO.getPositive())
                .setTotal(emotionDTO.getTotal());
        if (Validator.isNotEmpty(emotionDTO.getPeriodTime())) {
            builder.setTime(periodTimeDTOToGrpcResult(emotionDTO.getPeriodTime()));
        }
        return builder.build();
    }

    /**
     * 词云图数据结果转换为grpc结果
     *
     * @param workCloudDataDTOS 词云图数据
     * @return List<WordCloudData> grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:42
     **/
    public static List<WordCloudData> workCloudDataDTOToGrpcResult(List<WorkCloudDataDTO> workCloudDataDTOS) {
        List<WordCloudData> wordCloudDataList = new ArrayList<>();
        for (WorkCloudDataDTO workCloudDataDTO : workCloudDataDTOS) {
            WordCloudData build = WordCloudData.newBuilder().setKeyword(workCloudDataDTO.getKeyword()).setCount(workCloudDataDTO.getCount()).build();
            wordCloudDataList.add(build);
        }
        return wordCloudDataList;
    }

    /**
     * 话题引用率结果转换为grpc结果
     *
     * @param topicReferenceRateDTO 话题引用率结果
     * @return TopicReferenceRate grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:42
     **/
    public static TopicReferenceRate topicReferenceRateDTOToGrpcResult(TopicReferenceRateDTO topicReferenceRateDTO) {
        TopicReferenceRate.Builder builder = TopicReferenceRate.newBuilder().setCurPeriod(periodTimeDTOToGrpcResult(topicReferenceRateDTO.getCurPeriod()))
                .setLastPeriod(periodTimeDTOToGrpcResult(topicReferenceRateDTO.getLastPeriod()));
        for (ReferenceRateDTO referenceRateDTO : topicReferenceRateDTO.getRefRates()) {
            builder.addRefRates(referenceRateDTOToGrpcResult(referenceRateDTO));
        }
        return builder.build();
    }

    /**
     * 引用率结果转换为grpc结果
     *
     * @param referenceRateDTO 引用率结果
     * @return ReferenceRate grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:43
     **/
    public static ReferenceRate referenceRateDTOToGrpcResult(ReferenceRateDTO referenceRateDTO) {
        ReferenceRate.Builder builder = ReferenceRate.newBuilder();
        builder.setId(referenceRateDTO.getId()).setName(referenceRateDTO.getName())
                .setCurNum(referenceRateDTO.getCurNum())
                .setCurTotalNum(referenceRateDTO.getCurTotalNum())
                .setCurRatio(referenceRateDTO.getCurRatio().intValue())
                .setLastNum(referenceRateDTO.getLastNum())
                .setLastTotalNum(referenceRateDTO.getLastTotalNum())
                .setLastRatio(referenceRateDTO.getLastRatio().intValue())
                .setGrowthRatio(referenceRateDTO.getGrowthRatio().intValue());
        return builder.build();
    }

    /**
     * 话题关联结果转换为grpc结果
     *
     * @param topicRelationDTOS 话题关联结果
     * @return List<TopicRelation> grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:43
     **/
    public static List<TopicRelation> topicRelationDTOToGrpcResult(List<TopicRelationDTO> topicRelationDTOS) {
        List<TopicRelation> topicRelationList = new ArrayList<>();
        for (TopicRelationDTO topicRelationDTO : topicRelationDTOS) {
            TopicRelation build = TopicRelation.newBuilder()
                    .setName(topicRelationDTO.getName())
                    .setCount(topicRelationDTO.getCount())
                    .setTotal(topicRelationDTO.getTotal())
                    .setRatio(topicRelationDTO.getRatio())
                    .build();
            topicRelationList.add(build);
        }
        return topicRelationList;
    }

    /**
     * 话题提及率变化趋势结果转换为grpc结果
     *
     * @param topicStatDTOs 话题提及率变化趋势
     * @return List<TopicStat> grpc结果
     * @author jy.zhao
     * @date 2021/7/27 18:49
     **/
    public static List<TopicStat> topicReferenceTrendDTOToGrpcResult(List<TopicStatDTO> topicStatDTOs) {
        List<TopicStat> topicStats = new ArrayList<>();
        for (TopicStatDTO topicStatDTO : topicStatDTOs) {
            TopicStat build = TopicStat.newBuilder()
                    .setCount(topicStatDTO.getCount())
                    .setTotal(topicStatDTO.getTotal())
                    .setRatio(topicStatDTO.getRatio())
                    .setTime(periodTimeDTOToGrpcResult(topicStatDTO.getPeriodTime()))
                    .build();
            topicStats.add(build);
        }
        return topicStats;
    }

    /**
     * 观点评价变化趋势结果转换为grpc结果
     *
     * @param viewpointEvaluateStatDTOS 观点评价变化趋势
     * @return List<ViewpointEvaluateStat> grpc结果
     * @author jy.zhao
     * @date 2021/7/27 21:58
     **/
    public static List<ViewpointEvaluateStat> viewpointEvaluateStatDTOToGrpcResult(List<ViewpointEvaluateStatDTO> viewpointEvaluateStatDTOS) {
        List<ViewpointEvaluateStat> viewpointEvaluateStats = new ArrayList<>();
        for (ViewpointEvaluateStatDTO viewpointEvaluateStatDTO : viewpointEvaluateStatDTOS) {
            ViewpointEvaluateStat build = ViewpointEvaluateStat.newBuilder()
                    .setNegative(viewpointEvaluateStatDTO.getNegative())
                    .setNeutral(viewpointEvaluateStatDTO.getNeutral())
                    .setPositive(viewpointEvaluateStatDTO.getPositive())
                    .setTime(periodTimeDTOToGrpcResult(viewpointEvaluateStatDTO.getPeriodTime()))
                    .build();
            viewpointEvaluateStats.add(build);
        }
        return viewpointEvaluateStats;
    }


    /**
     * 周期时间转换为grpc结果
     *
     * @param periodTimeDTO 周期时间
     * @return PeriodTime grpc结果
     * @author jy.zhao
     * @date 2021/6/23 15:44
     **/
    public static PeriodTime periodTimeDTOToGrpcResult(PeriodTimeDTO periodTimeDTO) {
        return PeriodTime.newBuilder().setStartTime(DateUtil.getTimestamp(periodTimeDTO.getStartTime()))
                .setEndTime(DateUtil.getTimestamp(periodTimeDTO.getEndTime()))
                .build();
    }

}
